package com.example.xui.Main;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xui.Article;
import com.example.xui.Post;
import com.example.xui.R;
import com.example.xui.View.Login.LoginView;
import com.google.gson.JsonArray;
import com.yalantis.phoenix.PullToRefreshView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class ThreeFragment extends Fragment {
    RecyclerView mRecyclerView;
    MyAdapter mMyAdapter ;
    private PullToRefreshView mPullToRefreshView;
    String sno;
    private MyAdapter myAdapter;
    List<JSONObject> list1=new ArrayList<>();
   //   List<News> mNewsList = new ArrayList<>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_three, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        sno=getActivity().getIntent().getStringExtra("sno");
        init();
        id_one(sno);
    }

    public void init(){
        com.getbase.floatingactionbutton.FloatingActionButton floatingActionButton=getActivity().findViewById(R.id.action_zi);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), Post.class);
                intent.putExtra("sno",sno);
                startActivity(intent);
            }
        });
        com.getbase.floatingactionbutton.FloatingActionButton floatingActionButton1=getActivity().findViewById(R.id.tianzi);
        floatingActionButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(getContext(), Article.class);
                intent.putExtra("sno",sno);
                startActivity(intent);
            }
        });
        mRecyclerView = getActivity().findViewById(R.id.recyclerView1);
        mMyAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mMyAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(layoutManager);
        mPullToRefreshView = (PullToRefreshView) getActivity().findViewById(R.id.pull_to_refresh);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPullToRefreshView.setRefreshing(false);
                    }
                }, 2);
            }
        });
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    public static ThreeFragment newInstance() {
        ThreeFragment fragment1 = new ThreeFragment();
        return fragment1;
    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {

        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(getContext(), R.layout.adapter_note, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
            if(!list1.equals("[]")) {
                try {
                    Log.i("tag","tag="+list1.get(position).getString("resourcePicture"));
                    holder.pictureto.setImageBitmap(BitmapFactory.decodeFile(list1.get(position).getString("resourcePicture")));
                //    holder.mTitleContent.setText(list1.get(position).getString("resourceContent"));
                    holder.mTitleid.setText(list1.get(position).getString("resourceNickname"));
                    holder.mTitledTitle.setText(list1.get(position).getString("resourceTitle"));
                    holder.classification.setText(list1.get(position).getString("resourceType"));
                    holder.likenum.setText(list1.get(position).getString("resourceThumbNum"));
                    holder.commentnum.setText(list1.get(position).getString("resourceConmentNum"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public int getItemCount() {
            int a=0;
            //return list1.size();
            if(!list1.equals("[]")){
                a=list1.size();
            }
            return a;
        }
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView mTitleid;
      //  TextView mTitleContent;
        ImageView pictureto;
        TextView mTitledTitle;
        TextView favorite;
        TextView comment;
        TextView data;
        TextView classification;
        TextView likenum;
        TextView commentnum;

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            pictureto=itemView.findViewById(R.id.imageView17);
            mTitleid = itemView.findViewById(R.id.deviceName);
          //  mTitleContent = itemView.findViewById(R.id.deviceAddress);
            mTitledTitle=itemView.findViewById(R.id.textView9);
            favorite=itemView.findViewById(R.id.textView51);
            comment=itemView.findViewById(R.id.textView53);
            classification=itemView.findViewById(R.id.textView47);
            data=itemView.findViewById(R.id.textView54);
            likenum = itemView.findViewById(R.id.textView51);
            commentnum = itemView.findViewById(R.id.textView53);
        }
    }

    public void id_one(String id){
        new Thread(){
            @Override
            public void run() {
                super.run();
                RequestParams params = new RequestParams("http://10.138.87.49:8080/resource/resoucrceselectID");
                params.addHeader("Content-Type","application/x-www-form-urlencoded");
                params.addBodyParameter("resourceUserID",id);
                x.http().post(params, new Callback.CacheCallback<JSONArray>() {
                    @Override
                    public void onSuccess(JSONArray result) {
                        List<JSONObject> list = new ArrayList<>();
                        for (int i = 0; i < result.length(); i++) {
                            try {
                                JSONObject jsonObject = result.getJSONObject(i);
                                list.add(jsonObject);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    list1=list;
                                    Log.i("tag","种类="+list1);
                                    myAdapter = new MyAdapter();
                                    mRecyclerView.setAdapter(myAdapter);
                                    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                                    mRecyclerView.setLayoutManager(layoutManager);
                                }

                            });

                        }

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public boolean onCache(JSONArray  result) {
                        return false;
                    }
                });
            }
        }.start();
    }
}
